package com.sun.dailyprj.pki;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.sun.dailyprj.R;
import com.sun.dailyprj.databinding.ActivityPkiLayoutBinding;
import com.sun.dailyprj.util.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import sun.security.pkcs.PKCS7;

public class PkiActivity extends AppCompatActivity {
    private ActivityPkiLayoutBinding mBinding;
    private static final String sSignPath = "sdcard/carota/sign/759f0be961462ea5dbe8b8b95b0f93c1";
    private static final String sPackagePath = "sdcard/carota/sign/fe22a66c6f56315c857596f606c1fbe1";
    private static final String signDataPath = "sdcard/carota/sign/19d296e56ab3c5875d1d60c17082d746";

    private String ALGORITHMSTR = "RSA/NONE/PKCS1Padding";
    private String ALGORITHMSTR_ECB = "RSA/ECB/PKCS1Padding";
    private String ALGORITHMSTR_E = "SHA1withRSA";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_pki_layout);
        mBinding.setPki(this);



    }

    public void onClick(View view) {
        Logger.debug("onClick()");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    SignParser parser = new SignParser(new File(sSignPath));
                    Bundle bundle = parser.parse();
                    String signStr = bundle.getString(SignParser.PROP_SIGN);
                    Logger.debug("type = " + parser.getType() + " ; sha256 = " + signStr);

                    FileInputStream fis = new FileInputStream(new File(sPackagePath));
                    String sha256 = "0c59524593071ce183690aff09abe8dcd27c3f77e55d43c50852e0daf323ac98";

                    boolean result = PKCSUtil.signedDataVerify(EncryptHelper.decryptBASE64Byte(signStr), sha256 );
                    Logger.debug("result = " + result);

//                    boolean res = PKiUtils.signedDataVerify(signStr.getBytes(), "1905a84e1f1b7daafabba6e41865ee5a0acf67f38985334300fd43ef2ba70749");
//                    Logger.debug("res = " + res);

                    boolean res2 = PKCSUtil.signedDataVerify1(EncryptHelper.decryptBASE64Byte(signStr));
                    Logger.debug("res2 = " + res2);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    public void onClickEncode(View view) {
        new Thread(()->{
            String sha256 = "0c59524593071ce183690aff09abe8dcd27c3f77e55d43c50852e0daf323ac98";
            String oldSha256Encode = "DFlSRZMHHOGDaQr/Cavo3NJ8P3flXUPFCFLg2vMjrJg=";
            try {
                String newSha256 = EncryptHelper.encryptBASE64(sha256);
                Logger.debug("newSha256 = " + newSha256 + "\n" + " oldSha256 = " + oldSha256Encode);
                FileInputStream fis = new FileInputStream(signDataPath);
                byte[] b = new byte[fis.available()];
                fis.read(b);
                Logger.debug("b = " + (b == null ? 0 : b.length));
                PKCSUtil.signedDataVerify(EncryptHelper.decryptBASE64Byte(b), sha256);

            } catch (Exception e) {
                e.printStackTrace();
            }


        }).start();
    }
}
